package in.muton.edushare.pages.cp.backup.restore;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.panels.upload.uploadify.UploadifyBehavior;
import in.muton.edushare.panels.upload.uploadify.UploadifyPanel;
import in.muton.edushare.wicket.models.CayenneModel;

import java.io.IOException;
import java.util.ArrayList;

import nu.xom.ParsingException;
import nu.xom.ValidityException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.Model;

import com.edushare.common.backup.SystemBackupUtils;
import com.edushare.common.dao.SystemBackupDAO;
import com.edushare.common.vo.SystemBackup;

public class UploadFilesPanel extends EdusharePanel {

	private static final long serialVersionUID = 6853334100753832004L;
	private static Log log = LogFactory.getLog(UploadFilesPanel.class);

	public UploadFilesPanel(String id, final CayenneModel<SystemBackup> bkpModel, final EduShareBasePage page, final RestorePanel restorePanel) {
		super(id, bkpModel, page);
		
		final Form<Void> form = new Form<Void>("toMediaUpload"){

			private static final long serialVersionUID = -2804476494215761903L;

			@Override
			protected void onSubmit() {
				try {
					ArrayList<String> lst = SystemBackupUtils.validateConfigFile(bkpModel.getObject());
					restorePanel.loadPanel(new UploadMediaPanel(RestorePanel.PLACE_HOLDER_PANEL_ID, bkpModel, new Model<ArrayList<String>>(lst), page, restorePanel));
				} catch (ValidityException e) {
					log.error("Error processing config file", e);
				} catch (ParsingException e) {
					log.error("Error processing config file", e);
				} catch (IOException e) {
					log.error("Error processing config file", e);
				}
			}
		};
		add(form);
		form.setOutputMarkupPlaceholderTag(true);
		form.setVisible(false);
		
		Form<Void> cancelForm = new Form<Void>("cancelUpload"){

			private static final long serialVersionUID = 5061366414136959981L;

			@Override
			protected void onSubmit() {
				new SystemBackupDAO(getORMInterface()).delete(bkpModel.getObject());
				getORMInterface().commitAllChanges();
				restorePanel.loadPanel(new ChooseMethodPanel(RestorePanel.PLACE_HOLDER_PANEL_ID, restorePanel.univModel, page, restorePanel));
			}
		};
		add(cancelForm);
		
		UploadifyBehavior behav = new UploadifyBehavior(ConfigUploadProcessPage.class, EduShareSession.get().getId());
		behav.setFileExt("*.xml");
		behav.setFileDesc("xml files");
		behav.setSizeLimit(""+1000*1000*1000);
		behav.setScriptData("'bkpId':'"+ bkpModel.getObject().getSystemBackupId() +"'");
		add(new UploadifyPanel("configDiv", page, behav));
		AbstractDefaultAjaxBehavior behavior = new AbstractDefaultAjaxBehavior() {
			
			private static final long serialVersionUID = -59011156835917123L;

			@Override
			protected void respond(AjaxRequestTarget target) {
				form.setVisible(true);
				target.addComponent(form);
			}
			
			@Override
			public void renderHead(IHeaderResponse response) {
				super.renderHead(response);
				CharSequence callback = getCallbackScript();
				response.renderJavascript("function uploadCompleted(){ " + callback +" } ", "Custom Upload Completed");
			}

		};
		
		add(behavior);
	}

}
